package com.ms.teapuesto.util;

import com.ms.teapuesto.LogServer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * User: RAC
 * Date: 25/05/12
 */

public class SQLClientImpl implements SQLClient {

    public static final Logger LOG = LoggerFactory.getLogger(SQLClientImpl.class);

    @Autowired
    DataSource dataSource;

    SQLClienteDef sqlClienteDef;

    private String database;

    @PostConstruct
    public void init() throws SQLException {
        Connection connection=null;
        try {
            connection=dataSource.getConnection();
            String bd = connection.getMetaData().getDatabaseProductName();
            if (bd.startsWith("DB2")) {
                database="DB2";
                sqlClienteDef = new Db2ClientImpl();
            } else if (bd.startsWith("Oracle")) {
                database="Oracle";
                sqlClienteDef = new OracleClientImpl();
            } else if (bd.startsWith("PostgreSQL")) {
                database="PostgreSQL";
                sqlClienteDef = new PgClientImpl();
            } else {

            }
        } catch (SQLException e) {
            LogServer.log.error("Error en el sistema", e);
        }finally {
            DAOUtils.close(connection);
        }
    }

    public String toNumber(String campo) {
        return sqlClienteDef.toNumber(campo);
    }
    public String trunctDate(String campo){
        return sqlClienteDef.trunctDate(campo);
    }

    public String getDate() {
        return sqlClienteDef.getDate();
    }

    public String getDate(int hours) {
        return sqlClienteDef.getDate(hours);
    }

    class Db2ClientImpl implements SQLClienteDef {
        public String toNumber(String secuencialDocumento) {
            return " cast(" + secuencialDocumento + " as integer ) ";

        }

        public String getDate() {
            return null;  //Todo implementar
        }

        public String trunctDate(String campo) {
            return " VARCHAR_FORMAT("+campo+", 'YYYY-MM-DD') ";
        }

        public String getDate(int hours) {
            return " (current time - " + hours + " hour) ";
        }

        public Integer getNextSequence() {
            return 0;
        }
    }

    class OracleClientImpl implements SQLClienteDef {
        public Integer getNextSequence() {
            return 0;
        }

        public String toNumber(String secuencialDocumento) {
            return " to_number(" + secuencialDocumento + ") ";
        }

        public String trunctDate(String campo) {
            return " to_char("+campo+", 'YYYY-MM-DD') ";
        }

        public String getDate() {
            return " sysdate ";

        }

        public String getDate(int hours) {
            return " (sysdate - " + hours / 24+") ";
        }
    }

    class PgClientImpl implements SQLClienteDef {

        public Integer getNextSequence() {
            return 0;
        }

        public String toNumber(String secuencialDocumento) {
            return " to_number(" + secuencialDocumento + ",'99999999') ";
        }

        public String trunctDate(String campo) {
            return " to_char("+campo+", 'YYYY-MM-DD') ";
        }

        public String getDate() {
            return " current_date ";

        }

        public String getDate(int hours) {
            return " (current_date - " + hours / 24+") ";
        }
    }

    public String getBd() {
        return database;
    }

    public Integer getNextSequence(){
        return sqlClienteDef.getNextSequence();
    }
}
